package javafx.com;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

public class main2 extends Application {

	public static void main(String[] args) {
		launch(args);
	}

	@Override
	public void start(Stage stage) throws Exception {

		Button button = new Button("button");
		
		// 只能管理孩子,孙子不能管理
		AnchorPane anchorPane = new AnchorPane();
		AnchorPane anchorPane2 = new AnchorPane();
		
		// 是否管理节点
//		button.setManaged(false);
		
		anchorPane2.getChildren().add(button);
		anchorPane2.setRightAnchor(button, 0.0);
		anchorPane2.setBottomAnchor(button, 0.0);

		anchorPane.getChildren().addAll(anchorPane2);

		anchorPane.setStyle("-fx-background-color:#FF3E96");
		anchorPane2.setStyle("-fx-background-color:#FF4E6D");

		Scene scene = new Scene(anchorPane);

		stage.setScene(scene);
		stage.setTitle("javvfx");
		stage.setWidth(600);
		stage.setHeight(600);

		stage.show();

		// 设置大小,anchorPane的大小在stage的大小确定后才能确定
		anchorPane.setTopAnchor(anchorPane2, 0.0);
		anchorPane.setLeftAnchor(anchorPane2, 0.0);
		anchorPane.setRightAnchor(anchorPane2, anchorPane.getWidth() / 2);
		anchorPane.setBottomAnchor(anchorPane2, anchorPane.getHeight() / 2);

		// 设置监听,实现动态大小
//		stage.heightProperty().addListener((number) -> {
//			anchorPane.setBottomAnchor(anchorPane2, anchorPane.getHeight() / 2);
//		});
//		stage.widthProperty().addListener((number) -> {
//			anchorPane.setRightAnchor(anchorPane2, anchorPane.getWidth() / 2);
//		});

		stage.heightProperty().addListener(new ChangeListener<Number>() {

			@Override
			public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
				anchorPane.setBottomAnchor(anchorPane2, anchorPane.getHeight() / 2);
			}
		});

		stage.widthProperty().addListener(new ChangeListener<Number>() {

			@Override
			public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
				anchorPane.setRightAnchor(anchorPane2, anchorPane.getWidth() / 2);
			}
		});
		
		/**
		 * lambda表达式的效果和匿名函数的效果,居然不一样,有点奇怪了
		 */

	}

}
